package com.student.Activitys.http.manager;

import android.app.Activity;
import android.util.Log;

import com.student.Activitys.bean.CarouselBean;
import com.student.Activitys.bean.CollegeSurveyBean;
import com.student.Activitys.bean.HomeNoticeBean;
import com.student.Activitys.bean.InfoReturnBean;
import com.student.Activitys.bean.ScheduleTestingBean;
import com.student.Activitys.bean.ScheduleWeekBean;
import com.student.Activitys.bean.pack.ImageTextJsonPack;
import com.student.Activitys.bean.pack.ScheduleTestingJsonPack;
import com.student.Activitys.bean.pack.StageListJsonPack;
import com.student.Activitys.http.ApiService;
import com.student.Activitys.http.NetWorkMgr;
import com.student.Activitys.http.api.HomeAPI;
import com.student.Activitys.utils.Constant;

import org.simple.eventbus.EventBus;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * 项目名称：
 * 类描述：所有 首页 接口管理 manager
 * 创建人：张飞祥
 * 创建时间：2017/9/27 0027 下午 1:33
 * 修改人：张飞祥
 * 修改时间：2017/9/27 0027 下午 1:33
 * 修改备注：
 */
public class HomeManager {

    private static HomeAPI mHomeAPI;
    private static ApiService mApiService;
    private static Activity mActivity;
    private static int mTaskId;

    //加载数据类型
    private int RefType = Constant.FIRST_LOAD;

    private static class HomeApiManagerHolder {
        private static final HomeManager INSTANCE = new HomeManager();
    }
    public static final HomeManager getHomeApiManager(Activity activity, int taskId) {
        mHomeAPI = NetWorkMgr.getInstance(activity)
                .getServiceAPI(HomeAPI.class);
        mApiService = ApiService.getApiService();
        mActivity = activity;
        mTaskId = taskId;
        return HomeApiManagerHolder.INSTANCE;
    }
    public void getType(int type){
        RefType=type;
    }


    /**
     *
     * 首页 轮播图 数据处理请求
     *
     */
    public void carouselList(String action) {
        Subscription subscription = mHomeAPI
                .carouselList(action).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<CarouselBean>>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_HOME_CAROUSEL_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"CarouselList(首页：轮播图) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<CarouselBean> list) {
                        //返回数据处理
                        if (list != null)
                            EventBus.getDefault().post(list, Constant.BUS_HOME_CAROUSEL_LIST);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 首页 通知公告 数据处理请求
     *
     */
    public void notice(String action,String type) {
        Subscription subscription = mHomeAPI
                .notice(action,type).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HomeNoticeBean>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_HOME_NOTICE_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"notice(首页：通知公告) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(HomeNoticeBean bean) {
                        //返回数据处理
                        if (bean != null)
                            EventBus.getDefault().post(bean, Constant.BUS_HOME_NOTICE);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 首页 校内动态 数据处理请求
     *
     */
    public void homeDynamic(String action,String index,String pageCount) {
        Subscription subscription = mHomeAPI
                .homeDynamic(action,index,pageCount).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ImageTextJsonPack>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_HOME_DYNAMIC_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"homeDynamic(首页：校内动态) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(ImageTextJsonPack pack) {
                        //返回数据处理
                        if (pack != null) {
                            if(RefType== Constant.FIRST_LOAD){
                                EventBus.getDefault().post(pack, Constant.BUS_HOME_DYNAMIC_NOTICE);
                            }else{
                                EventBus.getDefault().post(pack, Constant.BUS_HOME_DYNAMIC_FOOT_NOTICE);
                            }
                        }else{
                            Log.e(mActivity.getClass().getSimpleName(), "数据源为空");
                        }

                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 首页 通知公告 数据处理请求
     *
     */
    public void homeCollegeSurvey(String action,String title) {
        Subscription subscription = mHomeAPI
                .homeCollegeSurvey(action,title).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<CollegeSurveyBean>>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_HOME_COLLEGE_SURVEY_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"notice(首页：学院概况) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<CollegeSurveyBean> list) {
                        //返回数据处理
                        if (list != null)
                            EventBus.getDefault().post(list, Constant.BUS_HOME_COLLEGE_SURVEY);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 首页 阶段评价 评价列表 数据处理请求
     *
     */
    public void stageSee(String action,String xh) {
        Subscription subscription = mHomeAPI
                .stageSee(action,xh).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<StageListJsonPack>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_STAGE_SEE_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"stageEvaluateDateList(首页：阶段评价列表) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(StageListJsonPack pack) {
                        //返回数据处理
                        if (pack != null)
                            EventBus.getDefault().post(pack, Constant.BUS_STAGE_SEE_STAGE);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }


    /**
     *
     * 首页 阶段评价 刷新当前页面（星星列表） 数据处理请求
     *
     */
    public void stageSeeLoad(String action){
        Subscription subscription = mHomeAPI
                .stageSeeLoad(action).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ScheduleTestingBean>>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_HOME_STAGE_LOAD_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"stageSeeLoad(阶段评价 初始化加载) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<ScheduleTestingBean> list) {
                        //返回数据处理
                        if (list != null)
                            EventBus.getDefault().post(list, Constant.BUS_HOME_STAGE_LOAD);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }


    /**
     *
     * 首页 阶段评价 提交 数据处理请求
     *
     */
    public void stageEvaluate(String action,String pjzl,String xnxq,String xh,String jsh,String kch,String pjly,
                              String wd_ids,String fs) {
        Subscription subscription = mHomeAPI
                .stageEvaluate(action,pjzl,xnxq,xh,jsh,kch,pjly,wd_ids,fs).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<InfoReturnBean>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_STAGE_SUBMIT_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"stageEvaluateDateList(阶段评价：数据提交) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(InfoReturnBean bean) {
                        //返回数据处理
                        if (bean != null)
                            EventBus.getDefault().post(bean, Constant.BUS_STAGE_SUBMIT_STAGE);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 首页 阶段评价 详情 数据处理请求
     *
     */
    public void stageSeeDetails(String action,String pjzl,String xnxq,String xh,String jsh,String kch){
        Subscription subscription = mHomeAPI
                .stageSeeDetails(action,pjzl,xnxq,xh,jsh,kch).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ScheduleTestingJsonPack>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_STAGE_SEE_DETAILS_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"stageSeeDetails(阶段评价 详情) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(ScheduleTestingJsonPack pack) {
                        //返回数据处理
                        if (pack != null)
                            EventBus.getDefault().post(pack, Constant.BUS_STAGE_SEE_DETAILS_STAGE);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 首页 加载签到信息
     *
     */
    public void homeSignMsg(String action,String xh){
        Subscription subscription = mHomeAPI
                .homeSignMsg(action,xh).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ScheduleWeekBean>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_HOME_SIGN_MSG_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"homeSignMsg(首页：签到信息) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(ScheduleWeekBean bean) {
                        //返回数据处理
                        if (bean != null)
                            EventBus.getDefault().post(bean, Constant.BUS_HOME_SIGN_MSG);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 首页 签到
     *
     */
    public void homeSign(String action,String xh,String jsh,String kch,String xnxq,
                         String skzc,String skxq,String skjc,String jxb){
        Subscription subscription = mHomeAPI
                .homeSign(action,xh,jsh,kch,xnxq,skzc,skxq,skjc,jxb).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<InfoReturnBean>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_HOME_SIGN_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"homeSignMsg(首页：签到) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(InfoReturnBean bean) {
                        //返回数据处理
                        if (bean != null)
                            EventBus.getDefault().post(bean, Constant.BUS_HOME_SIGN);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

}
